import {notDownloadCount} from "@/api/system/download";

const sessionCache = {
  set (key, value) {
    if (!sessionStorage) {
      return
    }
    if (key != null && value != null) {
      sessionStorage.setItem(key, value)
    }
  },
  get (key) {
    if (!sessionStorage) {
      return null
    }
    if (key == null) {
      return null
    }
    return sessionStorage.getItem(key)
  },
  setJSON (key, jsonValue) {
    if (jsonValue != null) {
      this.set(key, JSON.stringify(jsonValue))
    }
  },
  getJSON (key) {
    const value = this.get(key)
    if (value != null) {
      return JSON.parse(value)
    }
  },
  remove (key) {
    sessionStorage.removeItem(key);
  }
}
const localCache = {
  set (key, value) {
    if (!localStorage) {
      return
    }
    if (key != null && value != null) {
      localStorage.setItem(key, value)
    }
  },
  get (key) {
    if (!localStorage) {
      return null
    }
    if (key == null) {
      return null
    }
    return localStorage.getItem(key)
  },
  setJSON (key, jsonValue) {
    if (jsonValue != null) {
      this.set(key, JSON.stringify(jsonValue))
    }
  },
  getJSON (key) {
    const value = this.get(key)
    if (value != null) {
      return JSON.parse(value)
    }
  },
  remove (key) {
    localStorage.removeItem(key);
  }
}
/*实时获取 sessionStorage 的值*/
export function resetSetItem() {
	// console.log("获取未查看数量");
  //   // 创建一个StorageEvent事件
  //   var newStorageEvent = document.createEvent('StorageEvent');
  //   const storage = {
  //     setItem: function (k, val) {
  //       sessionStorage.setItem(k, val);
  //       // 初始化创建的事件
  //       newStorageEvent.initStorageEvent('setItem', false, false, k, null, val, null, null);
  //       // 派发对象
  //       window.dispatchEvent(newStorageEvent)
  //     }
  //   }
    // notDownloadCount().then((response) => {
			
			// console.log("获取未下载数量222");
      // return storage.setItem('notDownloadCount', response.data || 0);
			
    // })
		
    notDownloadCount().then((response) => {
			this.$store.commit("settings/SET_NOTVIEWCOUNT", response.data || 0);
		})
}



export default {
  /**
   * 会话级缓存
   */
  session: sessionCache,
  /**
   * 本地缓存
   */
  local: localCache
}
